home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / COSEmulator / COSEmulator- SRC / headers / GameUtilities.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-21  |  4.5 KB  |  197 lines  |  [TEXT/CWIE]

  1. #ifndef _GameUtilities_h_
  2. #define _GameUtilities_h_
  3.  
  4. #include "GameTypes.h"
  5.  
  6. #define    abs( x ) ( x > 0 ? x : -x )
  7.  
  8.  
  9. //tOwnership     SetOwner( char individual, char team );
  10. Boolean        GetTextFromUser( char    *message , char *text);
  11.  
  12. Boolean    CStrEqual( char *str1 , char *str2 );
  13.  
  14. #define    rToR( foo )    (*(Rect *)(& foo))
  15. #define    RTor( foo )    (*(rect *)(& foo))
  16.  
  17. // templates
  18.  
  19. /*----------------------------------------------------------------------------\
  20.  
  21.     SectPtRect
  22.  
  23. \----------------------------------------------------------------------------*/
  24. template <class P , class R >
  25. Boolean SectPtRect( P    &thePt, R &therect )
  26. {
  27.  
  28. if( ( thePt.x >= therect.left ) && ( thePt.x < therect.right ) )
  29.     if( ( thePt.y >= therect.top ) && ( thePt.y < therect.bottom ) )
  30.         return( true );
  31.  
  32. return( false );
  33.  
  34. }
  35.  
  36. /*----------------------------------------------------------------------------\
  37.  
  38.     MySectrect
  39. -- checks to see if two rects intersect each toher
  40. \----------------------------------------------------------------------------*/
  41.  
  42. template <class R >
  43. Boolean MySectRect( const R *firstrect, const R *secrect )
  44. {
  45.     if( firstrect->right <= secrect->left )
  46.         return false;
  47.         
  48.     if( firstrect->left >= secrect->right )
  49.         return false;
  50.         
  51.     if( firstrect->bottom <= secrect->top )
  52.         return false;
  53.         
  54.     if( firstrect->top >= secrect->bottom )
  55.         return false;
  56.         
  57. return true;    
  58. }
  59.  
  60. /*----------------------------------------------------------------------------\
  61.  
  62.     MyCropRect
  63.  
  64. - true = it was in side or partly in side
  65. - false = the two rects did not intersect at all
  66. a = the rect that is being croped
  67. b = the rect that is croping
  68. c = the croped rect
  69.  
  70. \----------------------------------------------------------------------------*/
  71.  
  72. template <class R >
  73. Boolean MyCropRect( const R *a, const R *b , R *c )
  74. {
  75.     
  76.     if( !MySectRect( a , b ) )
  77.         return false;
  78.         
  79.     if( a->left < b->left )
  80.         c->left = b->left;
  81.     else
  82.         c->left = a->left;
  83.  
  84.     if( a->right > b->right )
  85.         c->right = b->right;
  86.     else
  87.         c->right = a->right;
  88.         
  89.     if( a->top < b->top )
  90.         c->top = b->top;
  91.     else
  92.         c->top = a->top;
  93.         
  94.     if( a->bottom > b->bottom )
  95.         c->bottom = b->bottom;
  96.     else
  97.         c->bottom = a->bottom;
  98.         
  99.     return true;
  100. }
  101.  
  102. /*----------------------------------------------------------------------------\
  103.  
  104.     RectDistance
  105.     
  106.     - how far a rect is from each other - intersect = 0
  107.     - note the distance is squared
  108.  
  109. \----------------------------------------------------------------------------*/
  110.  
  111. template <class R >
  112. ulong RectDistance( const R &rectA, const R &rectB )
  113. {
  114.  
  115.     if( !MySectRect( &rectA , &rectB ) )
  116.     {
  117.     
  118.     // check to see if it is directly above or below
  119.         if(( rectB.left >= rectA.left) && (rectB.right <= rectA.right ) ||
  120.              (rectB.left <= rectA.left )&& (rectB.right > rectA.left) ||
  121.              (rectB.right >= rectA.right )&& (rectB.left < rectA.right) )
  122.         {
  123.             if( rectB.bottom < rectA.top )
  124.                 return( rectA.top - rectB.bottom );
  125.             else
  126.                 return( rectB.top - rectA.bottom );
  127.         }
  128.     // see if it is to the left or right
  129.         if(( rectB.top >= rectA.top) && (rectB.bottom <= rectA.bottom ) ||
  130.              (rectB.top <= rectA.top )&& (rectB.bottom > rectA.top) ||
  131.              (rectB.bottom >= rectA.bottom )&& (rectB.top < rectA.bottom) )
  132.         {
  133.             if( rectB.right < rectA.left )
  134.                 return( rectA.left - rectB.right );
  135.             else
  136.                 return( rectB.left - rectA.right );
  137.         }
  138.     // handle it if they are diagonal of each other now
  139.         short    x,y;
  140.         
  141.         x = rectA.left - rectB.right;
  142.         y = rectA.top - rectB.bottom;
  143.         
  144.         return( x * x + y * y );
  145.     }
  146.     
  147.     return 0;
  148. }
  149.  
  150. /*----------------------------------------------------------------------------\
  151.  
  152.     MySetRect
  153.     
  154. set the rect giving each point
  155. \----------------------------------------------------------------------------*/
  156.  
  157. template <class R , class S >
  158. void MySetRect( R *theRect , S l , S t , S r , S b )
  159. {
  160.     theRect->left = l;
  161.     theRect->right = r;
  162.     theRect->top = t;
  163.     theRect->bottom = b;
  164. }
  165.  
  166. /*----------------------------------------------------------------------------\
  167.  
  168.     MySetRectWH
  169.     
  170. Set the rect using widths and heights
  171. \----------------------------------------------------------------------------*/
  172.  
  173. template <class R , class S >
  174. void MySetRectWH( R *theRect , S l , S t , S width , S height )
  175. {
  176.     theRect->left = l;
  177.     theRect->right = l + width;
  178.     theRect->top = t;
  179.     theRect->bottom = t + height;
  180. }
  181.  
  182. /*----------------------------------------------------------------------------\
  183.  
  184.     MyOffSetRect
  185.     
  186. change rects pos by 
  187. \----------------------------------------------------------------------------*/
  188.  
  189. template <class R , class S >
  190. void MyOffSetRect( R *theRect , S x , S y )
  191. {
  192.     theRect->left += x;
  193.     theRect->right += x;
  194.     theRect->top += y;
  195.     theRect->bottom += y;
  196. }
  197. #endif